<?php
class RecordController extends CommonController
{
	public function index()
	{
		$recordCls = new RecordModel($this->uid);
		$recordData = $recordCls->getAll();
		$this->view->assign('title', '收支记录');
		$this->view->assign('RecordData', $recordData);
		$this->view->setPlace('content', 'place/record');
		$this->view->display();
	}
	
	/**
	 * 添加记录
	 */
	public function add()
	{
		$do = Functions::gpc('do', 'p');
		$itemCls = new IeitemsModel($this->uid);
		$accountCls = new AccountModel($this->uid);
		
		if($do == 'save')
		{
			$data['AccountID'] = Functions::sqlgpc('AccountID', 'p');
			$data['IeItem'] = (int)Functions::sqlgpc('IeItem', 'p');
			$accountInfo = $accountCls->getAccountByID($data['AccountID']);
			if(empty($accountInfo)) $this->_optComplete(false, '?m=record&a=add', '请选择账户');
			$ieItem = $itemCls->getById($data['IeItem']);
			if(empty($ieItem)) $this->_optComplete(false, '?m=record&a=add', '请选收支项目');
			$amount = (float)Functions::sqlgpc('Amount', 'p', 0);
			if($amount <= 0) $this->_optComplete(false, '?m=record&a=add', '请输入金额');
			
			$data['Income'] = 0;
			$data['Expend'] = 0;
			if($ieItem['ItemType'] == 1)
			{
			    $data['Income'] = $amount;
			}
			else
			{
			    $data['Expend'] = $amount;
			}

			$data['DiaryDate'] = strtotime(Functions::sqlgpc('DiaryDate', 'p', date('Y-m-d H:i:s')));
			$data['Remark'] = Functions::sqlgpc('Remark', 'p');
			$recordCls = new RecordModel($this->uid);
			if(!$recordCls->add($data))
			{
			    $this->_optComplete(false, '?m=record&a=add', '内部错误');
			}
			$this->_optComplete(true, '?m=record', '修改完成');
		}
		
		$account = $accountCls->getAccounts();
		$this->view->assign('AccountList', $account);
		$this->view->assign('IeItem', $itemCls->getTree());
	    if($this->isAjax)
		{
		    $this->view->setUseLayout(false);
		}
		else
		{
		    $this->view->setPlace('content', 'page/record/add');
		}
		$this->view->assign('title', '记录收支');
		$this->view->display();
	}
	
	/**
	 * 记录列表
	 */
	public function rlist()
	{
	    $aid = Functions::gpc('aid', 'g', 0);
	    $recordCls = new RecordModel($this->uid);
	    $recordData = $recordCls->getByAccount($aid);
		$this->view->assign('title', '记录');
		$this->view->assign('RecordData', $recordData);
		$this->view->setThemeFile('record/index.php');
		$this->view->setPlace('content', 'place/record');
		$this->view->display();
	}

	/**
	 * 修改记录
	 */
	public function amend()
	{
		$aid = Functions::gpc('aid', 'g', 0);
		$do = Functions::gpc('do', 'p');
		$recordCls = new RecordModel($this->uid);
		
		if($do == 'save')
		{
			$aid = Functions::gpc('aid', 'p');
			$income = (float)Functions::sqlgpc('Income', 'p', 0);
			$expend = (float)Functions::sqlgpc('Expend', 'p', 0);
			$res = $recordCls->amend($aid, $income, $expend);
			var_dump($res);
			if($res === -1)
			{
				$this->_optComplete(false, '?m=record', '记录不存在');
			}
			else if($res === -2)
			{
				$this->_optComplete(false, '?m=record', '对应的资产账户已被注销，该记录不能修改！');
			}
			else if(!$res)
			{
				$this->_optComplete(false, '?m=record', '系统错误！');
			}
			$this->_optComplete(true, '?m=record', '');
		}
		$record = $recordCls->getRecord($aid);
		if(empty($record)){$this->_optComplete(false, '?m=record', '记录不存在');}
		$accountCls = new AccountModel($this->uid);
		$account = $accountCls->getAccounts();
		$this->view->assign('AccountList', $account);
		$this->view->assign('RecordInfo', $record);
		if($this->isAjax)
		{
		    $this->view->setUseLayout(false);
		}
		else
		{
		    $this->view->setPlace('content', 'page/record/amend');
		}
		$this->view->assign('title', '修改收支');
		$this->view->display();
	}
	
	public function __destruct()
	{
		//echo 'index controller destruct!<br />';
	}
}